﻿Imports System.Data.SqlClient
Public Class Frm_Av_Mowakil
    Public Sub remplirLismohami()
        List_mohami.Items.Clear()
        Dim Str As String = ""
        Partager.dv.remplir_List_avocat_bureau()
        For Each dossAvo As dossAvoBur In Partager.dv.List_avocat_bureau
            For Each avo As Avocat_bureau In Partager.moi.List_avocat_bureau
                If dossAvo.idAvoBur = avo.Id Then
                    If avo.type <> "صاحب المكتب" Then
                        Str = avo.Num_prof
                    End If
                    Partager.line = New System.Windows.Forms.ListViewItem(New String() {"", Str, avo.Nom_a, avo.Prenom_a, avo.Id}, -1)
                    'il est indispensable d'ajouter chekHand false et true
                    Partager.checkHand = False
                    If dossAvo.nomme Then
                        Partager.line.Checked = True
                    Else
                        Partager.line.Checked = False
                    End If
                    Me.List_mohami.Items.AddRange(New System.Windows.Forms.ListViewItem() {Partager.line})
                    Partager.checkHand = True
                End If
            Next
        Next
    End Sub

    'Remplir le comboBox par le nom et le prenom avec un id
    Public Sub remplirMo7ami(ByVal comb As ComboBox)
        Partager.moi.remplir_List_avocat_bureau()

        Dim htClient As New Hashtable()
        Dim str As String = ""
        For Each clt As Avocat_bureau In Partager.moi.List_avocat_bureau
            str = clt.Nom_a & "  " & clt.Prenom_a
            htClient.Add(clt.Id, str)
        Next

        For Each ImageType As DictionaryEntry In htClient
            comb.Items.Add(ImageType)
        Next
        comb.DisplayMember = "value"
        comb.ValueMember = "key"
        If comb.Items.Count > 0 Then
            comb.SelectedIndex = 0
        End If
    End Sub


    Private Sub Btn_parc_num_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_parc_num.Click
        Me.Enabled = False
        Partager.rech = True
        Partager.type = Me.Name
        Frm_Rech_Avoc_Clients.Show()
    End Sub


    Private Sub List_mohami_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles List_mohami.SelectedIndexChanged
        For Each i As Integer In List_mohami.SelectedIndices
            Partager.idAvoDoss = CType(Partager.dv.List_avocat_bureau.Item(i), dossAvoBur).id
            Partager.nomMohami = List_mohami.SelectedItems(0).SubItems(2).Text & " " & List_mohami.SelectedItems(0).SubItems(3).Text
        Next
    End Sub

    Private Sub Btn_ajout_mohami_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_ajout_mohami.Click
        If Frm_Idafa_Mowakilin.Comb_marji3ona.SelectedIndex < 0 Then
            MsgBox("اختر الملف أولا", MsgBoxStyle.Critical, "خطأ في الإضافة")
        ElseIf CombNomAv.SelectedIndex < 0 Then
            MsgBox("اختر المحامي أولا", MsgBoxStyle.Critical, "خطأ في الإضافة")
        Else
            Try
                If CombNomAv.Text <> "" Then

                    If MsgBox("هل تريد فعلا إضافة هذا المحامي لهذا الملف ؟", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "إضافة") = MsgBoxResult.Yes Then
                        Partager.close()
                        Partager.deClt = DirectCast(CombNomAv.SelectedItem, DictionaryEntry)
                        Dim idClt As Integer = CInt(Partager.deClt.Key)
                        Partager.dossAvo = New dossAvoBur(Partager.dv.id, idClt, "1")
                        Partager.dv.Ajouter_avocat_bureau(Partager.dossAvo)
                        remplirLismohami()
                        MsgBox("تمت إضافة المحامي للملف بنجاح", MsgBoxStyle.Information, "نجاح")
                    End If

                End If
            Catch sqlEx As SqlException
                If sqlEx.Message.Contains("IX_Dossiers_Avocat_Bureau") Then
                    MsgBox("عفوا، هذا المحامي تمت إضافته لهذاالملف من قبل", MsgBoxStyle.Critical, "خطأ")
                Else
                    MsgBox(sqlEx.Message)
                End If
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End If
    End Sub
    Private Sub Btn_supp_mohami_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_supp_mohami.Click
        Try
            If List_mohami.SelectedIndices.Count > 0 Then
                If MsgBox("هل تريد فعلا حذف هذا الموكل من هذا الملف ؟", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "حذف") = MsgBoxResult.Yes Then


                    Partager.dossAvo = Partager.dv.List_avocat_bureau(List_mohami.SelectedIndices.Item(0))
                    Partager.dv.suprimmer_avocat_bureau(Partager.dossAvo.id)
                    Partager.dv.remplir_List_avocat_bureau()
                    remplirLismohami()
                    MsgBox("عملية الحذف تمت بنجاح", MsgBoxStyle.Information, "نجاح")
                End If
            Else
                MsgBox("اختر محامي من القائمة", MsgBoxStyle.Information, "خطأ")

            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub

    Private Sub List_mohami_ItemChecked(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckedEventArgs) Handles List_mohami.ItemChecked
        'Dim Partager.dossAvo As dossAvoBur = dv.List_avocat_bureau(List_mowakilin.SelectedIndices.Item(0))
        If Partager.checkHand Then
            Dim i As Integer = 0
            For Each item1 As ListViewItem In List_mohami.Items
                Partager.dossAvo = New dossAvoBur
                Partager.dossAvo = Partager.dv.List_avocat_bureau(i)
                Partager.dossAvo.nomme = item1.Checked
                Partager.dv.modifierAvoNomme(Partager.dossAvo)
                i += 1
            Next
        End If
    End Sub

    Private Sub Btn_mokhabara_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_mokhabara.Click
        If Frm_Idafa_Mowakilin.Comb_marji3ona.SelectedIndex >= 0 And List_mohami.SelectedIndices.Count > 0 Then
            Frm_idafat_mokhabara.MdiParent = Frm_Main
            Me.Enabled = False
            Frm_idafat_mokhabara.Show()
        Else
            MsgBox("يجب عليك اختيار الملف و المحامي معا", MsgBoxStyle.Critical, "خطأ")
        End If
    End Sub

    Private Sub Frm_Av_Mowakil_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        Frm_Idafa_Mowakilin.Enabled = True
    End Sub

    Private Sub Frm_Av_Mowakil_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        List_mohami.Columns(0).DisplayIndex = List_mohami.Columns.Count - 1
        remplirMo7ami(CombNomAv)
        remplirLismohami()
        Me.BringToFront()
    End Sub
End Class